package com.example.news_demo.service;

import com.example.news_demo.entity.*;
import com.example.news_demo.mapper.ArticleMapper;
import com.example.news_demo.mapper.UserMapper;
import com.example.news_demo.mapper.ViewLogMapper;
import com.example.news_demo.mapper.ViewMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.xml.crypto.Data;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.ConcurrentModificationException;
import java.util.Date;
import java.util.List;

@Service
public class ArticleService {

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private ViewMapper viewMapper;
    @Autowired
    private ViewLogMapper viewLogMapper;
    public void insertViewLog(HttpServletRequest request){
        User loginUser = (User)request.getSession().getAttribute("loginUser");
        if(loginUser==null)return;
        long uid = loginUser.getId();
        if(uid<0)return;
        System.out.println("id+++"+uid);
        long article_id = Integer.parseInt(request.getParameter("article_id"));

        System.out.println("aid+++"+article_id);
        String view_time = new Date().toString();
        System.out.println(view_time);


        ViewLog viewLog = new ViewLog();
        viewLog.setArticle_id(article_id);
        viewLog.setUser_id(uid);
        viewLog.setView_time(view_time);
        viewLogMapper.insertViewLog(viewLog);
    }
    public Article getOneById(int id){
        return articleMapper.getOneById(id);
    }

    public List<Article> getArticleListByWord (String word){
        return articleMapper.getArticleListByWord(word);
    }

    public List<Content> selectContent(){
        return articleMapper.selectContent();
    }
    public List<Article> getRandom (int n){
        int max = 100000;
        List <Article> res = new ArrayList<>();
        while(n!=0){
            int random = (int)(Math.random()*(max-1))+1;
            res.add(getOneById(random));
            n--;
        }
        return res;
    }

    public List<Article> getCorrelation (long n){
        List<Article> AllCorrelation = new ArrayList<>();
        List<String> CorrelationWords =  articleMapper.getWordsByCorrelation(n);
        for(String word : CorrelationWords){
            List<Article> Correlation = articleMapper.getArticleListByWordLimit3(word);
            AllCorrelation.addAll(Correlation);
        }
        return AllCorrelation;
    }

    public List<Article> getInterest(int user_id){
        List<View> viewList = viewMapper.getAllByUserId(user_id);
        if(viewList != null && viewList.size() > 0)
            return getCorrelation(viewList.get(0).getArticle_id());
        return null;
    }

    public List<Article> getArticleListByLike(String word){
        return articleMapper.getArticleListByLike(word);
    }
}
